﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MauiApp1
{
    public class ToolBarButton : GraphicsView
    {
        public ToolBarButton()
        {
            Drawable = new ToolBarButtonDrawable();
            EndInteraction += OnEndInteraction;            
        }

        public event EventHandler? Clicked;

        private bool IsClicked
        {
            get => (bool)GetValue(IsClickedProperty);
            set => SetValue(IsClickedProperty, value);
        }

        private static BindableProperty IsClickedProperty = BindableProperty.Create(
            nameof(IsClicked),
            typeof(bool),
            typeof(ToolBarButton),
            null,
            propertyChanged: (bindable, oldValue, newValue) =>
            {
                if (bindable is not ToolBarButton toolBarButton) return;
                if (toolBarButton.Drawable is not ToolBarButtonDrawable drawable) return;
                drawable.IsClicked = (bool)newValue;
                toolBarButton.Invalidate();
            });


        private async void OnEndInteraction(object? sender, TouchEventArgs e)
        {
            this.IsClicked = true;
            await Task.Delay(250);
            this.IsClicked = false;
            Clicked?.Invoke(sender, e);
        }

        public static BindableProperty ButtonTypeProperty = BindableProperty.Create(
            nameof(ButtonType),
            typeof(ToolBarButtonType),
            typeof(ToolBarButton),
            null,
            propertyChanged: (bindable, oldValue, newValue) =>
            {
                if (bindable is not ToolBarButton toolBarButton) return;
                if (toolBarButton.Drawable is not ToolBarButtonDrawable drawable) return;
                drawable.ToolBarButtonType = (ToolBarButtonType)newValue;
            });

        public ToolBarButtonType ButtonType
        {
            get => (ToolBarButtonType)GetValue(ButtonTypeProperty);
            set => SetValue(ButtonTypeProperty, value);
        }

        public static BindableProperty LineColorProperty = BindableProperty.Create(
            nameof(LineColor),
            typeof(Color),
            typeof(ToolBarButton),
            null,
            propertyChanged: (bindable, oldValue, newValue) =>
            {
                if (bindable is not ToolBarButton toolBarButton) return;
                if (toolBarButton.Drawable is not ToolBarButtonDrawable drawable) return;
                drawable.LineColor = (Color)newValue;                
            });

        public Color LineColor
        {
            get => (Color)GetValue(LineColorProperty);
            set => SetValue(LineColorProperty, value);
        }

        public static BindableProperty FillColorProperty = BindableProperty.Create(
            nameof(FillColor),
            typeof(Color),
            typeof(ToolBarButton),
            null,
            propertyChanged: (bindable, oldValue, newValue) =>
            {
                if (bindable is not ToolBarButton toolBarButton) return;
                if (toolBarButton.Drawable is not ToolBarButtonDrawable drawable) return;
                drawable.FillColor = (Color)newValue;
                toolBarButton.Invalidate();
            });

        public Color FillColor
        {
            get => (Color)GetValue(FillColorProperty);
            set => SetValue(FillColorProperty, value);
        }         
    }

    public enum ToolBarButtonType
    {
        Add,
        Edit      
    }
}
